<?php

class Project_manage  extends AdminManage
{
	private $_pagesize=10;//分页个数
	/**
	 * 初始化
	 */
	public function _init()
	{

	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{

		#审核类型
		$type= $this->getParam('type');
		switch($type) {
			case 'pass':
				$nodatainfo =  '没有相匹配的项目合作信息';
				break;
			case 'no_pass':
				$nodatainfo = '没有相匹配的项目合作信息';
				break;
			default:
				$nodatainfo = '没有找到相匹配的项目合作信息';
		}

		#获取参数
		$key = $this->getParam('kw');
//		if($key != null){
//			$key = Encode::decodeURI($key);
//		}

		//时间
		$start=$this->getParam('start');
		$end=$this->getParam('end');

		#组合查询语句
		$strWhere = '';

		if(empty($type)){
			$strCheckState = '  status in(0,1)';
		}else{
			#审核状态
			switch($type) {
				case 'pass':
					$checkState = 1;
					break;
				case 'no_pass':
					$checkState = 0;
					break;

			}

			$strCheckState = '  status = '.intval($checkState);

		}

		if(!empty($key)){
			$strWhereKey = ' and projectName like \'%'.$key.'%\''  ;
			//				$strWhereKey= "and title like '%$key%'";
		}

		if($key==0&&is_numeric($key)){
			$strWhereKey = ' and projectName like  \'%0%\' ';
		}

		if($start && $end){
			$start1=$start/1000;
			$end1=$end/1000+86400;
			$strWhereStart=' and pc_project.firstTime between  '.$start1. ' and ' .$end1  ;
		}


		#查询条件
		$strWhere = $strWhere.$strCheckState.$strWhereKey.$strWhereStart;

		#获取数据总数
		$total = DB::getRows('pc_project',$strWhere);

		$sql='select pc_project.id,projectName,countryCode,address,projectType,projectArea,constructionName,status,pc_project.firstTime from pc_project ,pc_project_product
             where '.$strWhere.'  and linkman!=\' \' and pc_project_product.projectId = pc_project.id group by pc_project.id    ';
		$listData=DB::select($sql);
//		print_r($listData);
		$total=	count($listData);



		/**分页操作 start**/
		$p = 1; //当前页码
		if (isset($_GET['p']) && is_numeric($_GET['p'])) {
			$p =intval($_GET['p']);
			if ($p<=1) {
				$p = 1;
			}
		}

		$totalPage = ceil($total/$this->_pagesize);//计算总页数
		$begin = ($p-1)*$this->_pagesize;//计算开始游标
		$limit = "$begin,$this->_pagesize";//计算分页



		$pageObj = new Dpage($total, $this->_pagesize, $p);
		$pageCss = $pageObj->pageCss("1");
		$pageform = $pageObj->outputPages(1,"<li>","</li>");

		/**分页操作 end**/


		#获取数据信息
//		$list = new publicGetDataModel();
//		$listData = $list->getData($where = $strWhere,$limit = $limit,$fields = 'id,projectName,countryCode,address,projectType,projectArea,constructionName,status,firstTime',$order = 'firstTime',$table='pc_project');

		$sql='select pc_project.id,projectName,countryCode,address,projectType,projectArea,constructionName,status,pc_project.firstTime from pc_project ,pc_project_product
             where '.$strWhere.'  and linkman!=\' \'  and pc_project_product.projectId = pc_project.id group by pc_project.id order by pc_project.firstTime desc  limit  '.$limit;
		$listData=DB::select($sql);

		if(empty($listData)) {
			if (isset($key)) {
				$nodatainfo = '没有找到相匹配的项目合作信息';
			}
		}

//DB::echoExeSql();
		if($listData){
			$i=0;
			foreach($listData  as $k=>$v){
				$i++;
				$listData[$k]['autoid']=$i;

					//地区处理
					if(!empty($listData[$k]['COUNTRYCODE'])){
						#获取地区信息
						#省份信息
						include_once(SYSTEM_PATH.'libs/data/province.lib.php');
						$strProvince = '';
						if( $listData[$k]['COUNTRYCODE']){
							$area_len = mb_strlen( $listData[$k]['COUNTRYCODE']);
							if($area_len==6){
								$strProvince = $Province2[ $listData[$k]['COUNTRYCODE']];
							}elseif ($area_len==9){
								$strProvince = $Province2[substr( $listData[$k]['COUNTRYCODE'],0,6)].$City[substr( $listData[$k]['COUNTRYCODE'],0,6)][ $listData[$k]['COUNTRYCODE']];
							}elseif ($area_len==12){
								$strProvince = $Province2[substr( $listData[$k]['COUNTRYCODE'],0,6)].$City[substr( $listData[$k]['COUNTRYCODE'],0,6)][substr( $listData[$k]['COUNTRYCODE'],0,9)].$County[substr( $listData[$k]['COUNTRYCODE'],0,9)][ $listData[$k]['COUNTRYCODE']];
							}
							$listData[$k]['AREAINFO'] = $strProvince;
//					print_r($strProvince);
						}



					}





			}
		}

		$p=$this->getParam('p');
		if($p){
			$p=$p;
		}else{
			$p=1;
		}

		#模板赋值
		$now=time();
		$this->assign('now',$now);
		$this->assign('classify_list',$listData);
//		print_r($listData);
		$this->assign('pageCss',$pageCss);//分页样式
		$this->assign('pageform',$pageform);//分页标签
		$this->assign('type',$type);
		$this->assign('p',$p);
		$this->assign('kw',$key);
		$this->assign('start',$start/1000);
		$this->assign('end',$end/1000);
		$this->assign('nodatainfo',$nodatainfo);




		$this->assign('menu_navbar','合作管理  -- <a href="'.$_GET['action'].'.html">项目合作列表</a>');
		$this->tpl_full_path('/admins/project_manage/index');
	}



/**
 * 详情页
 */
	protected function _view(){
		#登录状态可以看到本身所有状态的信息
		$intLoadId = $this->session()->getSession('uid');
		#获取到参数
		$p=$this->getParam('p');
		$intId = $this->getParam('id');
		if(empty($intId) || !is_numeric($intId)){
			$this->goTo404();
		}

		$where='';
		$where.='id = '.$intId;
		$strWhere=$where;
		$list = new publicGetDataModel();
		$projectData = $list->getData($where = $strWhere,$limit = 1,$fields = 'id,projectName,projectType,projectArea,countryCode,address,constructionName,status,companyName,linkman,mobile,tel,email,leaveWord,firstTime ',$order = 'id',$table='pc_project');
		if($projectData){
			$projectData = $projectData[0];
		}else{
			$this->goTo404('该信息不存在!');
		}



		if($projectData['COUNTRYCODE']){
			//地区处理
			if(!empty($projectData['COUNTRYCODE'])){
				#获取地区信息
				#省份信息
				include_once(SYSTEM_PATH.'libs/data/province.lib.php');
				$strProvince = '';
				if( $projectData['COUNTRYCODE']){
					$area_len = mb_strlen( $projectData['COUNTRYCODE']);
					if($area_len==6){
						$strProvince = $Province2[ $projectData['COUNTRYCODE']];
					}elseif ($area_len==9){
						$strProvince = $Province2[substr( $projectData['COUNTRYCODE'],0,6)].$City[substr( $projectData['COUNTRYCODE'],0,6)][ $projectData['COUNTRYCODE']];
					}elseif ($area_len==12){
						$strProvince = $Province2[substr( $projectData['COUNTRYCODE'],0,6)].$City[substr( $projectData['COUNTRYCODE'],0,6)][substr( $projectData['COUNTRYCODE'],0,9)].$County[substr( $projectData['COUNTRYCODE'],0,9)][ $projectData['COUNTRYCODE']];
					}
					$projectData['AREAINFO'] = $strProvince;
//					print_r($strProvince);
				}
			}
		}



#产品清单
		$where='';
		$where.='projectId = '.$intId;
//		$where.='  and checkState = 1';
		$strWhere=$where;
		$list = new publicGetDataModel();
		$projectProductData = $list->getData($where = $strWhere,$limit = '30',$fields = 'id,projectId,productType,productNum,productdAttr ',$order = 'productType',$table='pc_project_product');
		if($projectProductData){
			foreach($projectProductData as $k=>$v){
				$projectProductData[$k]['SERPRODUCTDATTR'] = unserialize($projectProductData[$k]['PRODUCTDATTR'] );
			}
		}else{
			$this->goTo404('该信息不存在!');
		}

	 #统计
		$countSql='select projectId,productType,sum(productNum) as num from pc_project_product where projectId='.$intId.' group by productType ';
		$count=DB::select($countSql);
//		print_r($count);



		$this->assign('projectData',$projectData);
		$this->assign('projectProductData',$projectProductData);
		$this->assign('count',$count);
//		print_r($projectData);





		//保存
		if(  $_POST['submit'] ){
//print_r($_REQUEST);
			$intLoadId = $this->session()->getSession('uid');
			$id=$this->getParam('id');
			$data['systemId'] = $intLoadId;#
			$data['status'] = $this->getParam('status');#
			$data['operateTime'] = time();#

			$sql = "update pc_project set systemId = '" . $data['systemId'] . "' ,  status='" . $data['status'] . "', operateTime='" . $data['operateTime'] . " '
			        where id = " . $id;


			if(DB::execute($sql)){
				$message="保存成功 ~！";
				$url='http://system.gzcanr.com/admins/project_manage-p-'.$p.'.html';
				$this->alertGoto($message,$url);
			}else{
				$this->alertBack("保存失败,请重试！");
			}



		}




		$this->assign('p',$p);

		$this->tpl_full_path('/admins/project_manage/view');

	}














}
?>
